import re

from rest_framework import serializers
from keep.models import StaffModel, Role, Resource, Resource_exp, PersonModel, WorkflowPermission, Workflow, Department, \
    Position, WorkflowApprovalNode, Task, Approval_Record


class ResourceSerializer(serializers.ModelSerializer):
    pname = serializers.CharField(source='pid.name',read_only=True)
    class Meta:
        model = Resource
        fields = '__all__'


class RoleSerializer(serializers.ModelSerializer):
    pname = serializers.CharField(source='pid.name',read_only=True)
    class Meta:
        model = Role
        fields = '__all__'

def checkout_phone(phone):
    if re.match(r'^1[3-9]\d{9}$', phone):
        return phone
    else:
        raise serializers.ValidationError("手机号码格式不正确")

class StaffSerializer(serializers.ModelSerializer):
    role_id = serializers.SerializerMethodField(read_only=True)
    def get_role_id(self, obj):
        return obj.roleid.name if obj.roleid else ""
    class Meta:
        model = StaffModel
        fields = '__all__'
        extra_kwargs = {
            'phone': {
                'required': True,
                'validators': [checkout_phone],
            }
        }
# 互斥序列化器
class Resource_expSerializer(serializers.ModelSerializer):
    res1_name = serializers.CharField(source='res1.name',read_only=True)
    res2_name = serializers.CharField(source='res2.name',read_only=True)
    class Meta:
        model = Resource_exp
        fields = '__all__'
# 用户表
class Person_infoSerializer(serializers.ModelSerializer):
    class Meta:
        model = PersonModel
        fields = ["id","user_id","nickname","real_name","phone","avatar","get_gender_display","birth_date","address","height","weight","occupation"]

# 工作流权限序列化器
class WorkflowPermission_Ser(serializers.ModelSerializer):
    role_name = serializers.CharField(source='role_id.name', read_only=True)
    workflow_name  = serializers.CharField(source='workflow_id.name', read_only=True)
    class Meta:
        model = WorkflowPermission
        fields = '__all__'

# 工作流序列化器
# 新建工作流表
class WorkFlowSerializer(serializers.ModelSerializer):
    class Meta:
        model = Workflow
        fields = '__all__'


# 审批节点序列化器
class WorkflowApprovalNodeSerializer(serializers.ModelSerializer):
    workflow_name = serializers.CharField(source='workflow_id.name', read_only=True)
    position_name = serializers.CharField(source='position_id.name', read_only=True)

    class Meta:
        model = WorkflowApprovalNode
        fields = '__all__'


# 部门 序列化器
class DepartmentSerializer(serializers.ModelSerializer):
    class Meta:
        model = Department
        fields = '__all__'

# 职位 序列化器
class PositionSerializer(serializers.ModelSerializer):
    class Meta:
        model = Position
        fields = '__all__'


# 任务序列化器
class TaskSerializer(serializers.ModelSerializer):
    staff_name = serializers.CharField(source='staffid.name', read_only=True)
    workflow_name = serializers.CharField(source='workflow_id.name', read_only=True)
    next_audit_name = serializers.CharField(source='next_audit.name', read_only=True)
    class Meta:
        model = Task
        fields = ["id", "name", "params", "workflow_id", "workflow_name", "staffid", "staff_name", "next_audit","next_audit_name", "task_status", "get_task_status_display", "level"]

# 审批记录表
class  Approval_RecordSerializer(serializers.ModelSerializer):
    audit_name=serializers.CharField(source='audit_user.name', read_only=True)
    class Meta:
            model = Approval_Record
            fields = '__all__'